package com.richman.view;

import java.awt.BorderLayout;
import org.json.*;
import java.awt.Canvas;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.SimpleDateFormat;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;

import javax.swing.JComboBox;

import org.eclipse.swt.SWT;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;

import com.richman.controller.NegativeRatioClient;
import com.richman.view.BetCentralPane.MyTableModel;

public class Ps38BrowserPanle extends JPanel{
	
	private  Vector<Object[]> detailsData = new Vector<Object[]>();	
	private  Vector<Object[]> detailsData1 = new Vector<Object[]>();	
	MyTableModel tableMode = new MyTableModel();
	JTable table = null;
	
	MyTableModel1 tableMode1 = new MyTableModel1();
	JTable table1 = null;
	
	String[] months = {"本月", "上月"};
	
	private JComboBox combo = null;
		
	public Ps38BrowserPanle(){
		setLayout(new BorderLayout(0, 0));
		JPanel funBtnPane = new JPanel();
		
		JButton loaddataButton = new JButton("账目");
		loaddataButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//TODO:
				updateOutlineData(NegativeRatioClient.betCtrMng.getPS38OutlineData(Integer.toString(combo.getSelectedIndex())));
				
			}
		});
		
		loaddataButton.setBounds(20, 10, 150, 20);		
		
		combo = new JComboBox(months);
		combo.setBounds(0, 0, 100, 20);


		
		combo.setSelectedIndex(0);
		
		funBtnPane.add(loaddataButton);		
		funBtnPane.add(combo);
		add(funBtnPane, BorderLayout.NORTH);
		
		
		
		
	    table = new JTable(tableMode);	    
	    table.getColumnModel().getColumn(0).setPreferredWidth(120);	    
	    table.setRowHeight(30);	
	    table.setFont(new java.awt.Font("黑体", Font.PLAIN, 15));
		JScrollPane scrollPane = new JScrollPane(table);
		add(scrollPane, BorderLayout.CENTER);
		
		
		//双击table事件
	    table.addMouseListener(new MouseAdapter(){

            public void mouseClicked(MouseEvent e) {
            	
            	
            	
          	  if(e.getClickCount() == 2){

              	  int row =((JTable)e.getSource()).rowAtPoint(e.getPoint()); //获得行位置 
              	  

//              	  if(Long.valueOf(detailsData.elementAt(row)[7].toString()) == 0){
//              		  return;
//              	  }
              	  
              	  
              	updateDetailsData(NegativeRatioClient.betCtrMng.getPS38DetailsData(detailsData.elementAt(row)[1].toString()));
              	  
          	  }else{
          		  return;
          	  }

            }
      });
		
		
	    table1 = new JTable(tableMode1);	    
	    table1.getColumnModel().getColumn(0).setPreferredWidth(120);
	    table1.setRowHeight(60);
	    table1.setFont(new java.awt.Font("黑体", Font.PLAIN, 15));
		JScrollPane scrollPane1 = new JScrollPane(table1);
		add(scrollPane1, BorderLayout.SOUTH);
		
		table.getColumnModel().getColumn(0).setPreferredWidth(10);
		table1.getColumnModel().getColumn(0).setPreferredWidth(10);
		
	}
	
	
	public void updateDetailsData(String dataStr) {
		
		try {
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

			detailsData1.clear();

			if(dataStr != null) {
				JSONArray ja = new JSONArray(dataStr);
				
				//{ "序号", "详细信息",  "选择队伍", "赔率", "注额", "输赢", "佣金", "状态"};
				
				for(int i = 0; i < ja.length(); i++) {
					
					JSONArray ja1 = ja.getJSONArray(i);
					
					Object [] objdata = new Object[8];
					objdata[0] = i + 1;
					
					String typestr = ja1.getString(31);
					if(typestr.equals("Soccer")) {
						typestr = "足球";
					}
					objdata[1] = "<html>" + ja1.getLong(7) + "<br/>" + typestr + "<br/>" + ja1.getString(14);
					objdata[2] = "<html>" + ja1.getString(22)  + ja1.getDouble(24) + "[" + ja1.getString(36) + "]" + 
							"<br/>" + ja1.getString(9) + "<br/>" + ja1.getString(28) + "@" + ja1.getString(15);
					
					objdata[3] = ja1.getDouble(25);
					objdata[4] = ja1.getDouble(29);
					objdata[5] = ja1.getDouble(0);
					objdata[6] = ja1.getDouble(1);
					
					String winLoseStr = "";
					if(ja1.getDouble(0) >0) {
						winLoseStr = "(赢)";
					}else if(ja1.getDouble(0) < 0) {
						winLoseStr = "(输)";
					}else {
						winLoseStr = "(已退款)";
					}
					
					String statusStr = ja1.getString(18);
					if(ja1.getString(18).equals("SETTLED")) {
						statusStr = "已解决" + winLoseStr;
					}
					
					String scoreStr = ja1.getString(4);
					String[] scoreArry = scoreStr.split(",");
					String halfScore = "上半场:";
					String wholeScore = "比赛:";
					if(scoreArry.length >1) {
						halfScore += scoreArry[0].split(":")[1];
						wholeScore += scoreArry[1].split(":")[1];
					}
					
					objdata[7] = "<html>" + statusStr + "<br/>" + 
							halfScore + "<br/>" + 
							wholeScore;
					


					detailsData1.add(objdata);
				}
			}else {
				JOptionPane.showMessageDialog(null,"投注数据获取失败，请稍后");
			}
			
			tableMode1.updateTable();
			
		}catch(Exception e) {
			e.printStackTrace();			
		}
		

	}
	
	
	public void updateOutlineData(String dataStr) {
		
		try {
			
			detailsData.clear();
			if(dataStr != null) {
				JSONArray ja = new JSONArray(dataStr);
				
				int index = 0;
				
				for(int i = 0; i < ja.length(); i++) {
					
					JSONArray ja1 = ja.getJSONArray(i);
					//{ "序号", "日期",   "流水", "收入/支出", "佣金", "现金余额"};
					

					Object [] objdata = new Object[6];
					
					if(ja1.getString(3).equals("OPEN_BAL")) {
						continue;
					}else if(ja1.getString(3).equals("BETTING")) {
						index++;
						objdata[0] = index;
						objdata[1] = ja1.getString(0);
						objdata[2] = ja1.getDouble(4);
						objdata[3] = ja1.getDouble(7);
						objdata[4] = ja1.getDouble(5);
						objdata[5] = ja1.getDouble(6);
					}


					detailsData.add(objdata);
				}
			}else {
				JOptionPane.showMessageDialog(null,"日结单数据获取失败，请稍后");
			}
			
			tableMode.updateTable();
			
		}catch(Exception e) {
			e.printStackTrace();			
		}
		

	}
	
	
	
    public class MyTableModel1 extends AbstractTableModel  
    {  
        /**
		 * 
		 */
		private static final long serialVersionUID = 99L;
		/* 
         * 这里和刚才一样，定义列名和每个数据的值 
         */  
		
        String[] columnNames =  
        { "序号", "详细信息",  "选择队伍", "赔率", "注额", "输赢", "佣金", "状态"};
        
       // { "序号", "联赛", "时间", "球队", "类型", "盘口", "皇冠", "PS38", "合并赔率","1","2","时间"};
        

  
        /** 
         * 构造方法，初始化二维数组data对应的数据 
         */  
        public MyTableModel1()  
        {  

        }  
  
        // 以下为继承自AbstractTableModle的方法，可以自定义  
        /** 
         * 得到列名 
         */  
        @Override  
        public String getColumnName(int column)  
        {  
            return columnNames[column];  
        }  
          
        /** 
         * 重写方法，得到表格列数 
         */  
        @Override  
        public int getColumnCount()  
        {  
            return columnNames.length;  
        }  
  
        /** 
         * 得到表格行数 
         */  
        @Override  
        public int getRowCount()  
        {         
	        if(null == detailsData1){
	    		return 0;
	    	}
            return detailsData1.size();  
        }  
  
        /** 
         * 得到数据所对应对象 
         */  
        @Override  
        public Object getValueAt(int rowIndex, int columnIndex)  
        {  
            //return data[rowIndex][columnIndex];
        	
        	if(detailsData1.size() == 0) {
        		return null;
        	}
        	
        	return detailsData1.elementAt(rowIndex)[columnIndex];
        }  
  
        /** 
         * 得到指定列的数据类型 
         */  
        @Override  
        public Class<?> getColumnClass(int columnIndex)  
        {  
        	
        	return String.class;
        	
//        	if(detailsData.size() == 0){
//        		return String.class;
//        	}
//
//            return detailsData.elementAt(0)[columnIndex].getClass();
        	
        }  
  
        /** 
         * 指定设置数据单元是否可编辑.这里设置"姓名","学号"不可编辑 
         */  
        @Override  
        public boolean isCellEditable(int rowIndex, int columnIndex)  
        {  
            return false;
        }  
          
        /** 
         * 如果数据单元为可编辑，则将编辑后的值替换原来的值 
         */  
        @Override  
        public void setValueAt(Object aValue, int rowIndex, int columnIndex)  
        {  
        	detailsData1.elementAt(rowIndex)[columnIndex] = (String)aValue;  
            /*通知监听器数据单元数据已经改变*/  
            fireTableCellUpdated(rowIndex, columnIndex);  
        }  
        
        public void updateTable(){
        	fireTableDataChanged();
        }
        
  
    }  
	
	
	
    public class MyTableModel extends AbstractTableModel  
    {  
        /**
		 * 
		 */
		private static final long serialVersionUID = 1L;
		/* 
         * 这里和刚才一样，定义列名和每个数据的值 
         */  
		
        String[] columnNames =  
        { "序号", "日期",   "流水", "收入/支出", "佣金", "现金余额"};
        
       // { "序号", "联赛", "时间", "球队", "类型", "盘口", "皇冠", "PS38", "合并赔率","1","2","时间"};
        

  
        /** 
         * 构造方法，初始化二维数组data对应的数据 
         */  
        public MyTableModel()  
        {  

        }  
  
        // 以下为继承自AbstractTableModle的方法，可以自定义  
        /** 
         * 得到列名 
         */  
        @Override  
        public String getColumnName(int column)  
        {  
            return columnNames[column];  
        }  
          
        /** 
         * 重写方法，得到表格列数 
         */  
        @Override  
        public int getColumnCount()  
        {  
            return columnNames.length;  
        }  
  
        /** 
         * 得到表格行数 
         */  
        @Override  
        public int getRowCount()  
        {         
	        if(null == detailsData){
	    		return 0;
	    	}
            return detailsData.size();  
        }  
  
        /** 
         * 得到数据所对应对象 
         */  
        @Override  
        public Object getValueAt(int rowIndex, int columnIndex)  
        {  
            //return data[rowIndex][columnIndex];
        	
        	if(detailsData.size() == 0) {
        		return null;
        	}
        	
        	return detailsData.elementAt(rowIndex)[columnIndex];
        }  
  
        /** 
         * 得到指定列的数据类型 
         */  
        @Override  
        public Class<?> getColumnClass(int columnIndex)  
        {  
        	
        	return String.class;
        	
//        	if(detailsData.size() == 0){
//        		return String.class;
//        	}
//
//            return detailsData.elementAt(0)[columnIndex].getClass();
        	
        }  
  
        /** 
         * 指定设置数据单元是否可编辑.这里设置"姓名","学号"不可编辑 
         */  
        @Override  
        public boolean isCellEditable(int rowIndex, int columnIndex)  
        {  
            return false;
        }  
          
        /** 
         * 如果数据单元为可编辑，则将编辑后的值替换原来的值 
         */  
        @Override  
        public void setValueAt(Object aValue, int rowIndex, int columnIndex)  
        {  
            detailsData.elementAt(rowIndex)[columnIndex] = (String)aValue;  
            /*通知监听器数据单元数据已经改变*/  
            fireTableCellUpdated(rowIndex, columnIndex);  
        }  
        
        public void updateTable(){
        	fireTableDataChanged();
        }
        
  
    }  
}
